package com.isoftstone.securityframework.device.local;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.stereotype.Service;

import com.isoftstone.securityframework.api.Permission;
import com.isoftstone.securityframework.device.DeviceGroupManager;
import com.isoftstone.securityframework.device.domain.Device;
import com.isoftstone.securityframework.device.domain.DeviceGroup;

@Service
@Transactional
public class DeviceGroupManagerLocalImpl implements DeviceGroupManager {

	@Resource
	private SqlSession session = null;
	
	
	@Override
	public DeviceGroup add(DeviceGroup group) {
		return null;
	}

	@Override
	public DeviceGroup modify(DeviceGroup group) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DeviceGroup remove(DeviceGroup group) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DeviceGroup remove(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DeviceGroup get(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DeviceGroup get(String groupName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DeviceGroup> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DeviceGroup> list(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void relDeviceGroup(Device Device, DeviceGroup[] groups) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void relDeviceGroup(Device Device, Serializable[] groupIds) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void relDeviceGroup(Serializable DeviceId, Serializable[] groupIds) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unrelDeviceGroup(Device Device, DeviceGroup[] groups) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unrelDeviceGroup(Device Device, Serializable[] groupIds) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unrelDeviceGroup(Serializable DeviceId, Serializable[] groupIds) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void relDeviceGroupPermission(DeviceGroup tg,
			Permission[] permissions) {
		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			com.isoftstone.securityframework.api.domain.Permission permission = (com.isoftstone.securityframework.api.domain.Permission) p;
			list.add(permission.getId());
		}
		this.relDeviceGroupPermission(tg.getId(), list.toArray(new Serializable[]{}));				
	}

	@Override
	public void relDeviceGroupPermission(DeviceGroup tg,
			Serializable[] permissions) {

		this.relDeviceGroupPermission(tg.getId(), permissions);				
	}

	@Override
	public void relDeviceGroupPermission(Serializable deviceGroupId,Serializable[] permissionIds) {
		if(deviceGroupId==null){
			throw new RuntimeException("deviceGroupId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("groupId", (String) deviceGroupId);
			params.put("permId",(String)id);
			session.insert("com.isoftstone.securityframework.DeviceGroupMapper.relDeviceGroupPermission",params);
			params.clear();
		}							
	}

	@Override
	public void unrelDeviceGroupPermission(DeviceGroup tg,
			Permission[] permissions) {

		List<Integer> list = new ArrayList<Integer>();
		for (Permission p : permissions) {
			com.isoftstone.securityframework.api.domain.Permission permission = (com.isoftstone.securityframework.api.domain.Permission) p;
			list.add(permission.getId());
		}
		this.unrelDeviceGroupPermission(tg.getId(), list.toArray(new Serializable[]{}));				
	}

	@Override
	public void unrelDeviceGroupPermission(DeviceGroup tg,
			Serializable[] permissions) {

		this.unrelDeviceGroupPermission(tg.getId(), permissions);				
	}

	@Override
	public void unrelDeviceGroupPermission(Serializable deviceGroupId,Serializable[] permissionIds) {
		if(deviceGroupId==null){
			throw new RuntimeException("deviceGroupId can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Serializable id : permissionIds) {
			params.put("groupId", (String) deviceGroupId);
			params.put("permId",(String)id);
			session.delete("com.isoftstone.securityframework.DeviceGroupMapper.unrelDeviceGroupPermission",params);
			params.clear();
		}								
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Permission> permissions(DeviceGroup group) {
		return session.selectList("com.isoftstone.securityframework.DeviceGroupMapper.listGroupPermission",group);
	}


}
